namespace Geekees.App.Cashbook.Web.Controls
{
	using System;
	using System.Collections;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.SessionState;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using Geekees.App.Cashbook.Lib;
	using Geekees.App.Cashbook.Lib.BLL;
	using Geekees.App.Cashbook.Lib.Entity;
	using Geekees.App.Cashbook.Lib.Helper;

	/// <summary>
	///		Summary description for SearchDashboard.
	/// </summary>
	public class SearchDashboard : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.DropDownList ddlClassification;
		protected System.Web.UI.WebControls.DropDownList ddlCategory;
		protected System.Web.UI.WebControls.TextBox txtCustomPeriodFrom;
		protected System.Web.UI.WebControls.TextBox txtCustomPeriodTo;
		protected System.Web.UI.WebControls.RadioButtonList rblOrderBy;
		protected System.Web.UI.WebControls.RadioButtonList rblOutcomeIncome;
		protected System.Web.UI.WebControls.RadioButtonList rblTime;

		public DateTime FromDate
		{
			get
			{
				object o = ViewState["FromDate"];
				return o == null ? Consts.MIN_DATE : (DateTime)o;
			}
			set
			{
				ViewState["FromDate"] = value;
			}
		}

		public DateTime ToDate
		{
			get
			{
				object o = ViewState["ToDate"];
				return o == null ? Consts.MAX_DATE : (DateTime)o;
			}
			set
			{
				ViewState["ToDate"] = value;
			}
		}

		public int ClassificationId
		{
			get
			{
				object o = ViewState["ClassificationId"];
				return o == null ? Consts.NO_ID : (int)o;
			}
			set
			{
				ViewState["ClassificationId"] = value;
			}
		}

		public int CategoryId
		{
			get
			{
				object o = ViewState["CategoryId"];
				return o == null ? Consts.NO_ID : (int)o;
			}
			set
			{
				ViewState["CategoryId"] = value;
			}
		}

		public CashItemType ItemType
		{
			get
			{
				object o = ViewState["ItemType"];
				return o == null ? CashItemType.None : (CashItemType)o;
			}
			set
			{
				ViewState["ItemType"] = value;
			}
		}

		public OrderByType OrderType
		{
			get
			{
				object o = ViewState["OrderType"];
				return o == null ? OrderByType.None : (OrderByType)o;
			}
			set
			{
				ViewState["OrderType"] = value;
			}
		}

		private void GetClassificationValue()
		{
			int result = Consts.NO_ID;
			if( this.ddlClassification.SelectedIndex > 0 )
			{
				try
				{
					result = int.Parse( this.ddlClassification.SelectedValue );
				}
				catch{}
			}

			this.ClassificationId = result;
		}

		private void GetCategoryValue()
		{
			int result = Consts.NO_ID;
			if( this.ddlCategory.SelectedIndex > 0 )
			{
				try
				{
					result = int.Parse( this.ddlCategory.SelectedValue );
				}
				catch{}
			}

			this.CategoryId = result;
		}

		private void GetItemType()
		{
			if( this.rblOutcomeIncome.SelectedValue == "1" )
				this.ItemType = CashItemType.Outcome;
			else if( this.rblOutcomeIncome.SelectedValue == "2" )
				this.ItemType = CashItemType.Income;
			else
				this.ItemType = CashItemType.None;
		}

		private void GetOrderType()
		{
			if( this.rblOrderBy.SelectedValue == "time" )
				this.OrderType = OrderByType.CreateTime;
			else if( this.rblOrderBy.SelectedValue == "amount" )
				this.OrderType = OrderByType.Amount;	
		}

		public void GetValue()
		{
			GetTimeValue();
			GetClassificationValue();
			GetCategoryValue();
			GetItemType();
			GetOrderType();
		}

		private void GetTimeValue()
		{
			#region if custom period
			if( this.txtCustomPeriodFrom.Text.Trim() != string.Empty && this.txtCustomPeriodTo.Text.Trim() != string.Empty )
			{
				DateTime customFromDate = Consts.MIN_DATE;
				DateTime customToDate = Consts.MIN_DATE;
				try
				{
					customFromDate = DateTime.Parse(this.txtCustomPeriodFrom.Text.Trim());
					customToDate = DateTime.Parse(this.txtCustomPeriodTo.Text.Trim());
				}
				catch{}

				if( customFromDate != Consts.MIN_DATE && customToDate != Consts.MIN_DATE )
				{
					this.FromDate = customFromDate;
					this.ToDate = customToDate;
					return;
				}
			}
			#endregion
			
			DateTime today = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day );

			switch( this.rblTime.SelectedValue.ToLower() )
			{
				
				case "all":
					this.FromDate = Consts.MIN_DATE;
					this.ToDate = Consts.MAX_DATE;
					break;
				case "year":
					DateTime yearFrom = new DateTime( today.Year, 1, 1 );
					DateTime yearTo = new DateTime( today.Year, 12, 31 );

					this.FromDate = yearFrom;
					this.ToDate = yearTo;
					break;
				case "month":
					this.FromDate = this.GetFirstDayOfMonth( today );
					this.ToDate = this.GetLastDayOfMonth( today );
					break;
				case "week":
					this.FromDate = this.GetFirstDayOfWeek(today );
					this.ToDate = this.GetLastDayOfWeek( today );
					break;
				case "day":
					this.FromDate = today;
					this.ToDate = today;
					break;
			}
		}

		#region Date Methods

		private DateTime GetFirstDayOfWeek( DateTime dtDate )
		{
			return dtDate.AddDays( - (int)dtDate.DayOfWeek );
		}

		private DateTime GetLastDayOfWeek( DateTime dtDate )
		{
			return dtDate.AddDays( 6 - (int)dtDate.DayOfWeek );
		}

  		/// <summary>
		/// Get the first day of the month for
		/// any full date submitted
		/// </summary>
		/// <param name="dtDate"></param>
		/// <returns></returns>
		private DateTime GetFirstDayOfMonth(DateTime dtDate)
		{
			// set return value to the first day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtFrom = dtDate;
			// remove all of the days in the month
			// except the first day and set the
			// variable to hold that date
			dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
 
			// return the first day of the month
			return dtFrom;
		}
        
		/// <summary>
		/// Get the first day of the month for a
		/// month passed by it's integer value
		/// </summary>
		/// <param name="iMonth"></param>
		/// <returns></returns>
		private DateTime GetFirstDayOfMonth(int iMonth)
		{
			// set return value to the last day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtFrom = new DateTime(DateTime.Now.Year, iMonth, 1);
 
			// remove all of the days in the month
			// except the first day and set the
			// variable to hold that date
			dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
 
			// return the first day of the month
			return dtFrom;
		}
  
		/// <summary>
		/// Get the last day of the month for any
		/// full date
		/// </summary>
		/// <param name="dtDate"></param>
		/// <returns></returns>
		private DateTime GetLastDayOfMonth(DateTime dtDate)
		{
			// set return value to the last day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtTo = dtDate;
 
			// overshoot the date by a month
			dtTo = dtTo.AddMonths(1);
 
			// remove all of the days in the next month
			// to get bumped down to the last day of the
			// previous month
			dtTo = dtTo.AddDays(-(dtTo.Day));
 
			// return the last day of the month
			return dtTo;
		}
  
		/// <summary>
		/// Get the last day of a month expressed by it's
		/// integer value
		/// </summary>
		/// <param name="iMonth"></param>
		/// <returns></returns>
		private DateTime GetLastDayOfMonth(int iMonth)
		{
 
			// set return value to the last day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtTo = new DateTime(DateTime.Now.Year, iMonth, 1);
 
			// overshoot the date by a month
			dtTo = dtTo.AddMonths(1);
 
			// remove all of the days in the next month
			// to get bumped down to the last day of the
			// previous month
			dtTo = dtTo.AddDays(-(dtTo.Day));
 
			// return the last day of the month
			return dtTo;
 
		}
 
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			if( !IsPostBack )
				BindData();
		}

		private void BindData()
		{
			Binder.BindClassification( this.ddlClassification, ListPromptType.All );
			BindCategory();
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.ddlClassification.SelectedIndexChanged +=new EventHandler(ddlClassification_SelectedIndexChanged);
		}
		#endregion

		private void ddlClassification_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindCategory();
		}

		private void BindCategory()
		{
			this.ddlCategory.Items.Clear();

			int selectedClassificationId = Consts.NO_ID;
			if( this.ddlClassification.SelectedIndex != 0 )
			{
				selectedClassificationId = int.Parse( this.ddlClassification.SelectedValue );
				Binder.BindCategory( this.ddlCategory, selectedClassificationId, ListPromptType.All );
			}
		}

	}
}
